package com.scaffolding.sophia.admin.biz.controller;

import com.scaffolding.sophia.admin.api.entity.user.User;
import com.scaffolding.sophia.admin.biz.service.authority.AuthorityService;
import com.scaffolding.sophia.common.base.support.ApiResponse;
import com.scaffolding.sophia.common.base.support.BaseController;
import com.scaffolding.sophia.common.security.service.SecurityUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author: yihur
 * @ProjectName: sophia_scaffolding
 * @Package: com.scaffolding.sophia.admin.biz.controller
 * @ClassName: AuthorityController
 * @Date: 2019/9/28 13:59
 * @Description:
 * @Version: 1.0
 */
@Slf4j
@RestController
@RequestMapping("/authority")
public class AuthorityController extends BaseController {

    @Autowired
    private AuthorityService authorityService;

    @GetMapping("/api/{id}")
    public ApiResponse getAuthorityByUserId(@PathVariable Long id) {
        return success(authorityService.findAuthorityByUserId(id));
    }

    @GetMapping("/getAuthority")
    public Object getAuthority(){
        User user = SecurityUtils.getUser();
        Authentication authentication = SecurityUtils.getAuthentication();
        List<Integer> roles = SecurityUtils.getRoles();
        Map<String, Object> map = new HashMap<>(4);
        map.put("user",user);
        log.info("用户信息user:{}",user);
        map.put("authentication",authentication);
        log.info("权限信息authentication:{}",authentication);
        map.put("roles",roles);
        log.info("角色信息roles:{}",roles);
        return map;
    }



}
